﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTH_DTO;
using QLTH_BUSINESS;
using unitlControl;

namespace QuanLy_THPT_S4.HocSinh
{
    public partial class frmHocSinh : Form
    {
        private readonly clsDanTocBU _dtbu;
        private readonly clsTonGiaoBU _tgbu;
        private readonly clsHocSinhBU _hsbu;
       private readonly clsPhanLoaiBU _plbu;
       private readonly clsHocSinhDTO _hsdto;


        public frmHocSinh()
        {
            InitializeComponent();
            _dtbu = new clsDanTocBU();
            _tgbu = new clsTonGiaoBU();
            _plbu = new clsPhanLoaiBU();
            _hsbu = new clsHocSinhBU();
            _hsdto = new clsHocSinhDTO();
        }


        private void DieuKhien(Boolean dk)
        {
            btnCapNhat.Enabled = dk;
            btnThemHS.Enabled = dk;
            btnXoa.Enabled = dk;
            btnImport.Enabled = dk;
        }
        private void frmHocSinh_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = _hsbu.DanhsachHS();

        }

        private void btnThemHS_Click(object sender, EventArgs e)
        {
            HocSinh.frmHosSinhUPDATE frmhs = new HocSinh.frmHosSinhUPDATE();
            DialogResult dr = frmhs.ShowDialog();
            if (dr == DialogResult.Cancel)
            {
                gridControl1.DataSource = _hsbu.DanhsachHS();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageboxUtils.YesNo("Bạn chắc muốn xóa học sinh này?") == DialogResult.Yes)
            {
                _hsdto.Mahs = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAHS").ToString();
                _hsbu.XoaHS(_hsdto);
                frmHocSinh_Load(sender, e);
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            HocSinh.frmHosSinhUPDATE FrmHS = new HocSinh.frmHosSinhUPDATE();
            FrmHS.Mahs = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAHS").ToString();
            int Rowhandle = gridView1.FocusedRowHandle;
            DialogResult dr = FrmHS.ShowDialog();
            if (dr == DialogResult.Cancel)
            {
                if (dr == DialogResult.Cancel)
                {
                    frmHocSinh_Load(sender, e);
                    gridView1.FocusedRowHandle = Rowhandle;
                }
            }
        }

        private void txtTimkiem_EditValueChanged(object sender, EventArgs e)
        {
            if (txtTimkiem.Text == "" || txtTimkiem.Text == null)
            {
                gridControl1.DataSource = _hsbu.DanhsachHS();
            }
            else
            {
                gridControl1.DataSource = _hsbu.TimKiemHSTheoDK(txtTimkiem.Text);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            int _record = 0;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String _path = ofd.FileName.ToString();
                DataTable DS_HS_Excel = new DataTable();
                _hsbu.Open_excel(_path);
                DS_HS_Excel = _hsbu.DanhSachHocSinhExcel();
                DieuKhien(false);
                progressBarControl1.Properties.ShowTitle = true;
                progressBarControl1.Properties.Step = 1;
                progressBarControl1.Properties.PercentView = false;
                progressBarControl1.Properties.Minimum = 0;
                progressBarControl1.Properties.Maximum = DS_HS_Excel.Rows.Count;
                if (DS_HS_Excel.Rows.Count > 1)
                {
                    for (int i = 0; i <= DS_HS_Excel.Rows.Count; i++ )
                    {
                        try 
                        {
                            _hsdto.Mahs = toancuc.Tao_ma(_hsbu.macuoi(), "HS", 5);
                            _hsdto.Holoths = DS_HS_Excel.Rows[i]["HOLOT"].ToString().Trim();
                            _hsdto.Tenhs = DS_HS_Excel.Rows[i]["TEN"].ToString().Trim();
                            if (DS_HS_Excel.Rows[i]["PHAI"].ToString().Trim() == "")
                            {

                            }
                            else
                            {
                                if (DS_HS_Excel.Rows[i]["PHAI"].ToString().Trim() == "Nam")
                                {
                                    _hsdto.Phai = 0;
                                }
                                else
                                {
                                    _hsdto.Phai = 1;
                                }
                            }
                            _hsdto.Diachi = DS_HS_Excel.Rows[i]["DIACHI"].ToString().Trim();
                            _hsdto.Noisinh = DS_HS_Excel.Rows[i]["NOISINH"].ToString().Trim();
                            _hsdto.Ngaynhaphoc = toancuc.ddMMyyyy(DS_HS_Excel.Rows[i]["NGAYNHAPHOC"].ToString().Trim());
                            _hsdto.Ngaysinh = toancuc.ddMMyyyy(DS_HS_Excel.Rows[i]["NGAYSINH"].ToString().Trim());
                            _hsdto.Tongiao.Matg = _tgbu.Laymatg(DS_HS_Excel.Rows[i]["TONGIAO"].ToString().Trim()).ToString();
                            _hsdto.Dantoc.Madt = _dtbu.Laymadt(DS_HS_Excel.Rows[i]["DANTOC"].ToString().Trim()).ToString();
                            _hsdto.Phanloai.Mapl = _plbu.LayMa_PL(DS_HS_Excel.Rows[i]["PHANLOAI"].ToString().Trim()).ToString();

                        }
                        catch { }

                        _record += _hsbu.ThemHS(_hsdto);
                        progressBarControl1.Show();
                        progressBarControl1.PerformStep();
                        progressBarControl1.Update();
                    }

                    MessageboxUtils.Success("Thêm thành công" + _record + "học sinh!");
                    DieuKhien(true);
                }
                frmHocSinh_Load(sender,e);

            }

            
        }
    }
}
